package easier.app.x.notifycation;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.text.TextUtils;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.BigTextStyle;
import androidx.core.app.NotificationCompat.Builder;
import androidx.core.app.NotificationManagerCompat;

/**
 * 创建通知
 */
public class NotificationBuilder {

    private final String mChannelId;

    public NotificationBuilder( Context context ) {

        this( context, context.getPackageName() );
    }

    public NotificationBuilder( Context context, String channelId ) {

        this( context, channelId, "Default Channel", "this is default channel" );
    }

    public NotificationBuilder( Context context, String channelId, String name, String description ) {

        mChannelId = channelId;
        createNotificationChannel( context, name, description );
    }

    private void createNotificationChannel( Context context, String name, String description ) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel( mChannelId, name, importance );
            channel.setDescription( description );
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = context.getSystemService( NotificationManager.class );
            notificationManager.createNotificationChannel( channel );
        }
    }

    public void build( Context context, int smallIcon, String title, String content, Intent intent, int notificationId ) {

        build( context, smallIcon, title, content, "", intent, notificationId );
    }

    public void build( Context context, int smallIcon, String title, String content, String bigText, Intent intent,
        int notificationId ) {

        NotificationCompat.Builder builder = new Builder( context, mChannelId )
            .setSmallIcon( smallIcon )
            .setContentTitle( title )
            .setContentText( content );

        if ( !TextUtils.isEmpty( bigText ) ) {
            builder.setStyle( new BigTextStyle().bigText( bigText ) )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT );
        } else {
            builder.setPriority( NotificationCompat.PRIORITY_DEFAULT );
        }

        PendingIntent pendingIntent = null;
        if ( intent != null ) {
            int flag;
            if ( VERSION.SDK_INT >= VERSION_CODES.M ) {
                flag = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
            } else {
                flag = PendingIntent.FLAG_UPDATE_CURRENT;
            }
            pendingIntent = PendingIntent.getActivity( context, 0, intent, flag );
        }

        if ( pendingIntent != null ) {
            builder.setContentIntent( pendingIntent );
            builder.setAutoCancel( true );
        }

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from( context );
        // notificationId is a unique int for each notification that you must define
        notificationManager.notify( notificationId, builder.build() );
    }

    public void cancel( Context context, int notificationId ) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from( context );
        notificationManager.cancel( notificationId );
    }

    public void cancelAll( Context context ) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from( context );
        notificationManager.cancelAll();
    }
}